"""
ASGI config for app project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import re_path
from app.chat import consumers

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')

websocket_urlpatterns = [
    re_path(r'^api/websocket/(?P<user_id>\w+)/(?P<username>\w+)$', consumers.UserSpecificConsumer.as_asgi()),
]

application = ProtocolTypeRouter({
    # http请求使用这个
    "http": get_asgi_application(),
    # websocket请求使用这个
    "websocket": AuthMiddlewareStack(
        URLRouter(
            websocket_urlpatterns
        )
    ),
})
